﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
namespace WPFExcelReport
{
    /// <summary>
    /// 1.外部传入设备类型，根据设备类型查找对应的所有设定
    /// 2.若勾选标准设备，根据选择的设备类型，成本中心，操作，找到标准设备，对标准设备产生一个指令集
    /// 3.将指令集和设备编号传入设定明细
    /// 4.明细根据标准设备产生指令集
    /// 5.指令集可以在设备之间自动复制
    /// 6.可以手工更改其中一个设备
    /// </summary>
    public partial class TMEKOPF
    {
        public TMEKOPF()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VMEKOPF();
            this.DataContext = MV;
            InitializeComponent();
        }

        public override void QueryCommand(object sender, RoutedEventArgs e)
        {

            TBMain.Clear();
            dwData = TMV.TBMain.NewRow();
            dwData["MEKMANDANT"] = app.Mandant;
            dwData["MEKMTYP"] = " AND MEKMTYP LIKE'" + KRS + "'";
            dwData["MEKAGANG"] = " AND MEKAGANG='" + LFD + "'";
            SResult = TMV.QMAE02(dwData);
            MergeData(SResult, false);
            base.QueryCommand(sender, e);
        }

        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!IsNew)
                {
                    IsNew = true;
                    DESS = "01";
                    return;
                }
                if (AGANG.Trim().Length < 2)
                {
                    throw new Exception("必须先输入操作类型");
                }
                if (AMTYP.Trim().Length < 2)
                {
                    throw new Exception("必须先输入设备类型");
                }
                COLORIT = "01";
                COLORIT = VSDDAT.SD22.GETNEXT("0022", DESS, AMTYP, 1, 2, 8);
                if (LFD.Trim().Length < 2)
                {
                    throw new Exception("必须输入设备");
                }
                if (DESS.Trim().Length < 2)
                {
                    throw new Exception("必须设定工艺设定");
                }
                if (COLORIT.Trim().Length < 2)
                {
                    throw new Exception("必须输入工艺");
                }
                dwData = TBMain.NewRow();
                TMV.SetUpNewRow(ref dwData); 
                dwData["MEKMANDANT"] = app.Mandant;
                dwData["MEKAGANG"] = AGANG.Substring(6, 15);
                dwData["MEKMTYP"] = AMTYP;
                if (IsByAM)
                {
                    dwData["MEKMNRKRS"] = KRS;
                    dwData["MEKMLFDNR"] = LFD;
                    dwData["MEKMPOS"] = POS;
                }
                else
                {
                    dwData["MEKMNRKRS"] = KSTKRS;
                    dwData["MEKMLFDNR"] = KST;
                    dwData["MEKMPOS"] = "  ";
                }
                dwData["MEKEVARHNR"] = DESS;
                dwData["MEKEVARUNR"] = COLORIT;
                TMV.Insert(dwData);
                vwMaster.View.MoveCurrentToFirst();
                IsNew = false;
                base.NewCommand(sender, e);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void DeleteCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DMAE01(dwMaster);
            filter = "MEKAGANG='" + dwMaster["MEKAGANG"].ToString() + "' AND MEKMTYP='" + dwMaster["MEKMTYP"].ToString() + "' AND MEKMNRKRS='" + dwMaster["MEKMNRKRS"].ToString()
                + "' AND MEKMLFDNR='" + dwMaster["MEKMLFDNR"].ToString()
                   + "' AND MEKMPOS='" + dwMaster["MEKMPOS"].ToString()
                      + "' AND MEKEVARHNR='" + dwMaster["MEKEVARHNR"].ToString()
                      + "' AND MEKEVARUNR='" + dwMaster["MEKEVARUNR"].ToString()
                + "'";
            rows = TBMain.Select(filter);
            vwMaster.View.MoveCurrentTo(-1);
            foreach (DataRow row in rows)
            {
                row.Delete();
            }
            TBMain.AcceptChanges();
            base.DeleteCommand(sender, e);
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            TMV.UpdateMEKOPF(dwMaster, 0);
            base.SaveCommand(sender, e);
        }
        public override void OnSTDMChg(string value)
        {
            base.OnSTDMChg(value);
        }

        public override void OnAMTYPChg(string value)
        {
            STDM = KSTKRS + KST + AMTYP;
            base.OnAMTYPChg(value);
        }

        public override void OnARTChg(string des)
        {
            if (ART.Length < 8)
                return;
            KRS = ART.Substring(0, 2);
            LFD = ART.Substring(2, 4);
            POS = ART.Substring(6, 2);
            base.OnARTChg(des);
        }
        private void btnPrint_Click(object sender, RoutedEventArgs e)
        {

        }

        public override void LOADBYAUF()
        {
            if (!IsSaved)
                return;
            if (ORDERNUM != KEY)
                return;
            if (KEY.Length < 35)
                return;
            dwData = TBMain.NewRow();
            dwData["MEKMANDANT"] = app.Mandant;
            dwData["MEKAGANG"] = "AND MEKAGANG= '" + ORDERNUM.Substring(0, 15) + "'";
            dwData["MEKMTYP"] = "AND MEKMTYP= '" + ORDERNUM.Substring(15, 8) + "'";
            dwData["MEKMNRKRS"] = "AND MEKMNRKRS= '" + ORDERNUM.Substring(23, 2) + "'";
            dwData["MEKMLFDNR"] = "AND MEKMLFDNR= '" + ORDERNUM.Substring(25, 4) + "'";
            dwData["MEKMPOS"] = "AND MEKMPOS= '" + ORDERNUM.Substring(29, 2) + "'";
            dwData["MEKEVARHNR"] = "AND MEKEVARHNR= '" + ORDERNUM.Substring(31, 2) + "'";
            dwData["MEKEVARUNR"] = "AND MEKEVARUNR= '" + ORDERNUM.Substring(33, 2) + "'";
            SResult = TMV.QMAE02(dwData);
            TBTemp = TBMain.Clone();
            TBTemp.Merge(SResult.Tables[0]);
            foreach (DataRow row in TBTemp.Rows)
            {
                dwMaster.ItemArray = row.ItemArray;
            }
            base.LOADBYAUF();
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (EXAUF.Length < 30)
                return;
            KSTKRS = EXAUF.Substring(24, 2);
            KST = EXAUF.Substring(26, 4);
            AMTYP = EXAUF.Substring(8, 8);
            dwData = TMV.TBMain.NewRow();
            dwData["MEKMANDANT"] = app.Mandant;
            dwData["MEKMTYP"] = "AND MEKMTYP= '" + AMTYP + "'";
            SResult = TMV.QMAE02(dwData);
            MergeData(SResult, true);
            base.OnEXAUFChg(vlaue);
        }

        public void GetMachineControls(object sender, RoutedEventArgs e)
        {
            MACHINEID = "";
            MACHINEID = KRS + LFD + POS;
            //触发TAM04STD进行更新
        }
        public override void OnMACHINEIDChg(string value)
        {
            if (KEY.Length < 31)
                return;
            if (value.Length < 24)
                return;
            if (KEY.Substring(23, 8) == value.Substring(16, 8))
                return;
            if (!MMACHINES.Contains(value + ","))
            {
                MMACHINES += value + ",";
            }
            base.OnMACHINEIDChg(value);
        }

        private void AE2ALLCommand(object sender, RoutedEventArgs e)
        {
            try
            {
                if (dwMaster == null)
                    throw new Exception("请先选择工艺");

                string[] ms = MMACHINES.Split(',');
                foreach (string m in ms)
                {
                    if (m.Length < 8) continue;
                    PM["MEKMNRKRS"] = m.Substring(0, 2);
                    PM["MEKMLFDNR"] = m.Substring(2, 4);
                    PM["MEKMPOS"] = m.Substring(6, 2);
                    PM["MEKAGANG"] = dwMaster["MEKAGANG"];
                    PM["MEKMTYP"] = dwMaster["MEKMTYP"];
                    PM["MEKEVARHNR"] = dwMaster["MEKEVARHNR"];
                    PM["MEKEVARUNR"] = dwMaster["MEKEVARUNR"];
                    TMV.DELAE(PM);

                }

                PM["MNRKRS"] = dwMaster["MEKMNRKRS"].ToString();
                PM["MLFDNR"] = dwMaster["MEKMLFDNR"].ToString();
                PM["MPOS"] = dwMaster["MEKMPOS"].ToString();
                PM["MTYP"] = dwMaster["MEKMTYP"].ToString();
                PM["AGANG"] = dwMaster["MEKAGANG"].ToString();
                PM["EVARHNR"] = dwMaster["MEKEVARHNR"].ToString();
                PM["EVARUNR"] = dwMaster["MEKEVARUNR"].ToString();
                SResult = TMV.AE05.LOADAE05(PM);
                DataTable TBAE = SResult.Tables[0].Copy();
                foreach (string m in ms)
                {
                    dwData = TBMain.NewRow();
                    dwData.ItemArray = dwMaster.ItemArray;
                    dwData["MEKMNRKRS"] = m.Substring(0, 2);
                    dwData["MEKMLFDNR"] = m.Substring(2, 4);
                    dwData["MEKMPOS"] = m.Substring(6, 2);
                    TMV.Insert(dwData);
                    foreach (DataRow dwAE in TBAE.Rows)
                    {
                        dwData = TBAE.NewRow();
                        dwData.ItemArray = dwAE.ItemArray;
                        dwData["MEVMNRKRS"] = m.Substring(0, 2);
                        dwData["MEVMLFDNR"] = m.Substring(2, 4);
                        dwData["MEVMPOS"] = m.Substring(6, 2);
                        TMV.AE05.InsertMEVAR(dwData);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }
    }
}
